<?php
namespace Admin\Controller;
use Think\Controller;
class SystemController extends Controller {
	public function __construct(){
        parent::__construct();
        $this->buser = M('user');
        $user_id = session('user_id');
    }

    public function nav(){
        $id = I('get.id');
        if($id) {
            $nav = M('nav')->where(array('parent_id'=>$id))->order(array('order','id'=>'desc'))->select();
        }else{
            $nav = M('nav')->where(array('parent_id'=>0))->order(array('order','id'=>'desc'))->select();
        }
        $this->id = $id;
        $this->nav = $nav;
        $this->display();
    }

    public function nav_add(){
        header("Content-type: text/html; charset=utf-8");
        $id = I('get.id');
        if(IS_POST){
            $id = I('get.id');
            $data = array();
            $data = I('post.arr');
            if($id){
                $data['level'] = 2;
            }else{
                $data['level'] = 1;
            }
            $data['parent_id'] = $id;
            // var_dump($data);die;
            if($data['name']){
                $res = M('nav')->add($data);
            }else{
                $this->error('请填写导航名称！',U('/system/nav_add').'?id='.$id);
                exit;
            }
            if($res){
                $this->success('添加成功！',U('/system/nav').'?id='.$id);
                exit;
            }else{
                $this->error('添加失败！',U('/system/nav_add').'?id='.$id);
                exit;
            }
        }
        $this->id = $id;
        $this->display();
    }

    public function nav_edit(){
        $nav_id = I('get.id');
        $nav = M('nav')->where(array('id'=>$nav_id))->select();
        if(IS_POST){
            $data = array();
            $data = I('post.arr');
            $res = M('nav')->where(array('id'=>$nav_id))->save($data);
            if($res){
                $this->success('修改成功！',U('/system/nav'));
                exit;
            }else{
                $this->error('修改失败！',U('/system/nav_edit',array('id'=>$nav_id)));
                exit;
            }
        }
        $this->nav = $nav;
        $this->display();
    }

    public function nav_del(){
        $id = I('get.id');
        $res = M('apply')->where(array('id'=>$id))->delete();
        if($res){
            $this->success('删除成功！',U('/Index/nav'),3);
        }else{
            $this->error('删除失败！',U('/Index/nav'),3);
        }
    }

    public function show(){
        $sql_name = I('post.name');
        $show = I('post.show');
        
    }
}